package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.ExcelCreateUserAction;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

public class ChangeQCardActivity extends BaseActivity {

    @BindView(R.id.et_user_qcard)
    EditText etUserQcard;
    @BindView(R.id.bt_queding)
    Button btQueding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_qcard);
        ButterKnife.bind(this);
        ChangLayTitle("身份证");
        init();
    }

    private void init() {
        btQueding.setClickable(false);
        etUserQcard.addTextChangedListener(this);
        etUserQcard.setText(getIntent().getStringExtra("qcard"));
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(etUserQcard).isEmpty()) {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
        } else {
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
        }
    }

    @OnClick(R.id.bt_queding)
    public void onClick() {
        if (TextUtils.isEmpty(etUserQcard.getText().toString())) {
            CommonUtil.showToask(ChangeQCardActivity.this, "请输入身份证号码！");
            return;
        }
        try {
            String idcard_mess = ExcelCreateUserAction
                    .IDCardValidate(etUserQcard.getText()
                            .toString());
            if (!TextUtils.isEmpty(idcard_mess)) {
                Toast.makeText(
                        ChangeQCardActivity.this,
                        ExcelCreateUserAction
                                .IDCardValidate(etUserQcard
                                        .getText().toString()),
                        Toast.LENGTH_SHORT).show();
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        getChangeData();
    }

    /**
     * 修改个人资料
     */
    private void getChangeData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.UpdatePerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangeQCardActivity.this, "uid"));
        mRequest.add("cardNo", etUserQcard.getText().toString());
        CallServer.getRequestInstance().add(ChangeQCardActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeQCardActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangeQCardActivity.this, "qcard", etUserQcard.getText().toString());
                        finish();

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangeQCardActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }
}
